﻿using System.Text;
using System.Web.Mvc;

namespace WS.EKA.Utilities.Extensions
{
    public static class HtmlPager
    {
        public static MvcHtmlString Pager(this HtmlHelper helper, int pageTotal, int currentPage,string controller)
        {
            var str = new StringBuilder();
            str.Append("<center>");
            str.Append("<div class='pagination'>");
            str.AppendFormat("<li><a href='/{0}/1'>Prev</a></li>", controller);

            int start = GetStartIndex(currentPage);
            int end = GetEndIndex(currentPage, pageTotal);
            for (int i = start; i <= end; i++)
            {
                if(i==currentPage)
                    str.AppendFormat("<li class='active'><a href='/{1}/{0}'>{0}</a></li>", i, controller);
                else
                    str.AppendFormat("<li><a href='/{1}/{0}'>{0}</a></li>", i, controller);
            }
            str.AppendFormat("<li><a href='/{1}/{0}'>Next</a></li>", pageTotal, controller);
            str.Append("</div>");
            str.Append("</center>");

            return MvcHtmlString.Create(str.ToString());
        }

        private static int GetStartIndex(int currentPage)
        {
            int index = currentPage - 5;
            if (index > 0)
                return index;
            else
                return 1;
        }
        public static int GetEndIndex(int currentPage,int pageTotal)
        {
            int index = currentPage + 5;
            if (index > pageTotal)
                return pageTotal;
            else
                return index;
        }
    }
}
